/* 
src/router router文件夹就是存放路由相关文件的

*/

import VueRouter from 'vue-router'
import Vue from 'vue'
// 导入login.vue
import login from '@/views/login/login.vue'
// 导入layout.vue
import layout from '@/views/layout/layout.vue'
import chart from '@/views/layout/chart/chart.vue'
import user from '@/views/layout/user/user.vue'
import question from '@/views/layout/question/question.vue'
import business from '@/views/layout/business/business.vue'
import subject from '@/views/layout/subject/subject.vue'


Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    {
      path: '/',
      // 重定向
      redirect: '/login'
    },
    {
      path: '/login',
      component: login,
      meta:{
        title: '登录',
        roles:['超级管理员','管理员','老师','学生']
      }
    },
    {
      path: '/layout',
      component: layout,
      redirect: './layout/chart',
      children: [
        {
          path: '/layout/chart',
          component: chart,
          meta:{
            icon:'el-icon-pie-chart',
            title: '数据概览',
            roles:['超级管理员','管理员','老师']
          }
        },
        {
          path: '/layout/user',
          component: user,
          meta:{
            icon:'el-icon-user',
            title: '用户列表',
            roles:['超级管理员','管理员']
          }
        },
        {
          path: '/layout/question',
          component: question,
          meta:{
            icon:'el-icon-edit-outline',
            title: '题库列表',
            roles:['超级管理员','管理员','老师']
          }
        },
        {
          path: '/layout/business',
          component: business,
          meta:{
            icon:'el-icon-office-building',
            title: '企业列表',
            roles:['超级管理员','管理员','老师']
          }
        },
        {
          path: '/layout/subject',
          component: subject,
          meta:{
            icon:'el-icon-notebook-2',
            title: '学科列表',
            roles:['超级管理员','管理员','老师','学生']
          }
        },
      ]
    }
  ]
})

import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
// 导入vuex
import store from '@/store'
// 导入message
import {Message} from 'element-ui'
// 导入删除token方法
import {removeEach} from '@/utils/local.js'
// 前置守卫：还没有出from的路由
router.beforeEach((to, from, next) => {
  Nprogress.start()
  // if(当前to的路由元中不包含当前登录人角色){
  //     提示一下
  //     删除token
  //     跳转登录页
  // }
  if(to.meta.roles.includes(store.state.role)){
    next()
  }else{
    Message.error('您无权访问该页面')
    removeLocal('token')
    next()//表示正常通过  不正常通过 next(path)
  }
})
// 后置守卫：已进入to的路由
router.afterEach((to, from) => {
  // to:要去的路由的路由信息，相当于响应路由的this.$router
  // from:从哪那的路由的路由信息，相当于路由的this.$router
  // window.console.log('to:', to)
  // window.console.log('from:', from)
  document.title = '黑马面面--' + to.meta.title
  Nprogress.done()
})

// 解决同一路由跳转报错 
const originalReplace = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalReplace.call(this, location).catch(err => err)
}




export default router